import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart'hide Action;
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';

import 'action.dart';
import 'state.dart';

Reducer<LoginState> buildReducer() {
  return asReducer(
    <Object, Reducer<LoginState>>{
      LoginAction.action: _onAction,
      LoginAction.checkAgree: _onCheckAgree,
      LoginAction.changeInputType: _onChangeInputType,
      LoginAction.onInputMobile: _onInputMobile,
      LoginAction.onInputCode: _onInputCode,
      LoginAction.onInputPwd: _onInputPwd,
      LoginAction.showPwd:_onChangeShowPwd,
    },
  );
}

LoginState _onCheckAgree(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState..checked = !newState.checked;
  if (newState.checked) {
    newState..checkResource = "images/server_check@2x.png";
  } else {
    newState..checkResource = "images/server_not_check@2x.png";
  }
  return newState;
}

LoginState _onAction(LoginState state, Action action) {
  final LoginState newState = state.clone();
  return newState;
}
LoginState _onChangeShowPwd(LoginState state, Action action) {
  final LoginState newState = state.clone();
  if(!newState.showPwd){
    newState..pwdCanSeeImg = "images/pwd_can_see@2x.png";
  }else{
    newState..pwdCanSeeImg = "images/pwd_cannot_see@2x.png";
  }
  return newState;
}

LoginState _onInputMobile(LoginState state, Action action) {
  final LoginState newState = state.clone();
  if (newState.mobile.length == 11) {
    newState..phoneImg = "images/phone_icon_select@2x.png";
  } else {
    newState..phoneImg = "images/phone_icon_unselect@2x.png";
  }
  if(newState.loginWayType==Constants.LOGIN_PWD_WAY){
    if(newState.mobile.length == 11&&newState.pwd.length>0){
      newState..loginTextColor = Colors.white
          ..loginBtnStartColor = AppColors.loginLeft
          ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }else{
    if(newState.mobile.length == 11&&newState.code.length>0){
      newState..loginTextColor = Colors.white
        ..loginBtnStartColor = AppColors.loginLeft
        ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }
  return newState;
}

LoginState _onInputCode(LoginState state, Action action) {
  final LoginState newState = state.clone();
  if(newState.loginWayType==Constants.LOGIN_PWD_WAY){
    if(newState.mobile.length == 11&&newState.pwd.length>0){
      newState..loginTextColor = Colors.white
        ..loginBtnStartColor = AppColors.loginLeft
        ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }else{
    if(newState.mobile.length == 11&&newState.code.length>0){
      newState..loginTextColor = Colors.white
        ..loginBtnStartColor = AppColors.loginLeft
        ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }
  return newState;
}

LoginState _onInputPwd(LoginState state, Action action) {
  final LoginState newState = state.clone();
  if (newState.pwd.isNotEmpty) {
    newState..pwdImg = "images/pwd_icon_select@2x.png";
  } else {
    newState..pwdImg = "images/pwd_icon_unselect@2x.png";
  }
  if(newState.loginWayType==Constants.LOGIN_PWD_WAY){
    if(newState.mobile.length == 11&&newState.pwd.length>0){
      newState..loginTextColor = Colors.white
        ..loginBtnStartColor = AppColors.loginLeft
        ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }else{
    if(newState.mobile.length == 11&&newState.code.length>0){
      newState..loginTextColor = Colors.white
        ..loginBtnStartColor = AppColors.loginLeft
        ..loginBtnEndColor = AppColors.loginRight;
    }else{
      newState..loginTextColor = AppColors.cantClickTextColor
        ..loginBtnStartColor = AppColors.loginBtnColor
        ..loginBtnEndColor = AppColors.loginBtnColor;
    }
  }
  return newState;
}

LoginState _onChangeInputType(LoginState state, Action action) {
  final LoginState newState = state.clone();
  if (newState.loginWayType == Constants.LOGIN_CODE_WAY)
    newState
      ..loginWayType = Constants.LOGIN_PWD_WAY
      ..loginWay = "验证码登录";
  else
    newState
      ..loginWayType = Constants.LOGIN_CODE_WAY
      ..loginWay = "密码登录";
  return newState;
}
